package com.component;

import com.Listener.ActionDoneListener;
import com.bean.Farmer;
import com.bean.Users;
import com.dao.FarmerDao;
import com.dao.UserDao;
import com.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddFarmerDialog extends JDialog {
    final int WIDTH = 500;
    final int HEIGHT = 400;

    private ActionDoneListener listener;

    public AddFarmerDialog(JFrame jf, String title, boolean isModel,ActionDoneListener listener){
        super(jf,title,isModel);
        this.listener  = listener;
        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装用户编号
        Box IDBox = Box.createHorizontalBox();
        JLabel IDLabel = new JLabel("农户编号：");
        JTextField IDField = new JTextField(15);

        IDBox.add(IDLabel);
        IDBox.add(Box.createHorizontalStrut(15));
        IDBox.add(IDField);

        //组装用户名
        Box nameBox = Box.createHorizontalBox();
        JLabel nameLabel = new JLabel("用  户  名：");
        JTextField nameField = new JTextField(15);

        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(15));
        nameBox.add(nameField);

        //组装农户姓名
        Box FarmerNameBox = Box.createHorizontalBox();
        JLabel FarmerNameLabel = new JLabel("农户姓名：");
        JTextField FarmerNameField = new JTextField(15);

        FarmerNameBox.add(FarmerNameLabel);
        FarmerNameBox.add(Box.createHorizontalStrut(15));
        FarmerNameBox.add(FarmerNameField);

        //组装登录密码
        Box passwordBox = Box.createHorizontalBox();
        JLabel passwordLabel = new JLabel("登陆密码：");
        JTextField passwordField = new JTextField(15);

        passwordBox.add(passwordLabel);
        passwordBox.add(Box.createHorizontalStrut(15));
        passwordBox.add(passwordField);

        //组装性别
        Box genderBox = Box.createHorizontalBox();
        JLabel genderLabel = new JLabel("  性     别：");
        JComboBox<String> ConsumerGenderBox = new JComboBox<>();
        ConsumerGenderBox.addItem("男");
        ConsumerGenderBox.addItem("女");

        genderBox.add(genderLabel);
        genderBox.add(Box.createHorizontalStrut(20));
        genderBox.add(ConsumerGenderBox);

        //组装家庭住址
        Box addressBox = Box.createHorizontalBox();
        JLabel addressLabel = new JLabel("家庭住址：");
        JTextField addressField = new JTextField(15);

        addressBox.add(addressLabel);
        addressBox.add(Box.createHorizontalStrut(15));
        addressBox.add(addressField);

        //组装联系电话
        Box phoneBox = Box.createHorizontalBox();
        JLabel phoneLabel = new JLabel("联系电话：");
        JTextField phoneField = new JTextField(15);

        phoneBox.add(phoneLabel);
        phoneBox.add(Box.createHorizontalStrut(15));
        phoneBox.add(phoneField);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton addBtn = new JButton("添加");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户的录入
                int UserID = Integer.parseInt(IDField.getText().trim());
                String Username = nameField.getText().trim();
                String Farmername = FarmerNameField.getText().trim();
                String Password = passwordField.getText().trim();
                String Gender = (String) ConsumerGenderBox.getSelectedItem();
                String Address = addressField.getText().trim();
                String PhoneNumber = phoneField.getText().trim();

                if(IDField.getText().trim().length()!=8){
                    JOptionPane.showMessageDialog(jf, "农户编号要为八位数字！");
                }else if(Password.length()<=8){
                    JOptionPane.showMessageDialog(jf, "农户密码需要8位以上!");
                }else if(!PhoneNumber.matches("\\d{11,12}")){
                    JOptionPane.showMessageDialog(jf, "联系电话是11或12位数字组成!");
                }else{
                    //将数据添加进数据库
                    Users user = new Users(UserID,"农户",Username,Password,Gender,PhoneNumber,"",Address);
                    new UserDao().insertUser(user);
                    Farmer farmer = new Farmer(UserID,Farmername,Password,Gender,Address,PhoneNumber);
                    new FarmerDao().insertFarmer(farmer);
                    JOptionPane.showMessageDialog(jf, "添加成功!");
                    listener.done(null);
                    dispose(); // 关闭添加窗口
                }
            }
        });


        btnBox.add(addBtn);

        vBox.add(Box.createVerticalStrut(20));
        vBox.add(IDBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(nameBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(FarmerNameBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(passwordBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(genderBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(addressBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(phoneBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(50));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(50));

        this.add(hBox);
    }
}
